package com.fzrskj.cloud.free.user.dao;

import com.fzrskj.cloud.free.core.utils.CloudDbDrvUtils;
import com.fzrskj.cloud.free.user.bean.UserSecGroupRule;
import com.winiis.component.common.utils.db.DbUtils;
import ms.core.consts.SysErr;
import ms.core.db.IDbDrv;
import ms.core.object.CObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserSecGroupRuleDao extends CObject{
	//private declarations
	private IDbDrv db=null;

	//constructor function to initialize database access object
	public UserSecGroupRuleDao(){
		db= CloudDbDrvUtils.getDbUtilsDrv();
	}

	//to free resource
	@Override
	public void dispose(){
		db.closeConnection();
	}

	//to add new record to database by bean
	public int addUserSecGroupRule(UserSecGroupRule obj){
		String sql = "insert into user_secgroup_rule(ruleId,direction,sgType,grantType,grantObject,startPort,"+
			"endPort,createTime,ruleDesc,userId,userName,sgId)"+
			" values(?,?,?,?,?,?,?,?,?,?,?,?);";
		Object[] prm={obj.getRuleId(),obj.getDirection(),obj.getSgType(),obj.getGrantType(),obj.getGrantObject(),obj.getStartPort(),
				obj.getEndPort(),obj.getCreateTime(),obj.getRuleDesc(),obj.getUserId(),obj.getUserName(),obj.getSgId()};

		int ret = db.executeSql(sql,prm);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		if (ret>0) {
			ret = DbUtils.lastId(this.db);
		}
		return ret;
	}

	//to add new record to database by map
	public int addUserSecGroupRule(Map<String, Object> values){
		int ret = db.insertRecord("user_secgroup_rule",values);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by id
	public int delUserSecGroupRule(int id){
		String sql="delete from user_secgroup_rule where id=?";
		int ret = db.executeSql(sql,new Object[]{id});
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to delete table record by cdt
	public int delUserSecGroupRule(Map<String, Object> cdt){
		int ret = db.deleteRecord("user_secgroup_rule",cdt);
		String code = db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	//to modify table record by values and condition
	public int modUserSecGroupRule(Map<String, Object> values, Map<String, Object> cdt){
		int ret = db.modifyRecord("user_secgroup_rule", values, cdt);
		String code = db.getErrCode();
		this.setErrCode(code);
		if (!code.equals(SysErr.CODE_SUCCESS)) {
			this.log(db.getMsg());
		}

		return ret;
	}

	//to query user_secgroup_rule item for detail
	public UserSecGroupRule queryUserSecGroupRule(int id){
		String sql="select * from user_secgroup_rule where id=?";
		UserSecGroupRule ret = db.queryForBean(sql,UserSecGroupRule.class,new Object[]{id});
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}

	/**
	 * 查询安全组规则列表
	 * @param cdt
	 * @param page
	 * @param pagesize
	 * @return
	 */
	public List<UserSecGroupRule> queryUserSecGroupRuleList(Map<String, Object> cdt, int page, int pagesize){
		StringBuffer sb = new StringBuffer("select * from user_secgroup_rule where 1=1 ");
		
		List<Object> list = new ArrayList<Object>();
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			sb.append(" and "+c.getKey()+"=?");
			list.add(c.getValue());
		}
		sb.append(" order by id asc"); 
		if ((page>0) && (pagesize>0)) {
			sb.append(String.format(" limit %d,%d",(page-1)*pagesize,pagesize));
		}
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		List<UserSecGroupRule> ret = db.queryForBeans(sb.toString(), UserSecGroupRule.class, params);
		this.setErrCode(db.getErrCode());
		return ret;
	}
	
	/**
	 * 统计安全组规则
	 * @param cdt
	 * @return
	 */
	public int queryUserSecGroupRuleCount(Map<String, Object> cdt){
		List<Object> list = new ArrayList<Object>();
		StringBuilder sb=new StringBuilder("select count(id) from user_secgroup_rule where 1=1 ");
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			sb.append(" and "+c.getKey()+"=?");
			list.add(c.getValue());
		}
		
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}

		Long ret = (Long) db.queryFieldValue(sb.toString(), Long.class, params, 1);
		this.setErrCode(db.getErrCode());
		return ret.intValue();
	}
	
	/**
	 * 查询安全组规则
	 * @param cdt
	 * @return
	 */
	public UserSecGroupRule queryUserSecGroupRule(Map<String, Object> cdt){
		StringBuilder sb=new StringBuilder("select * from user_secgroup_rule where 1=1 ");
		
		List<Object> list = new ArrayList<Object>();
		for(Map.Entry<String, Object> c: cdt.entrySet()){
			sb.append(" and "+c.getKey()+"=?");
			list.add(c.getValue());
		}
		
		Object[] params = new Object[list.size()];
		for (int i = 0; i < list.size(); i++) {
			params[i] = list.get(i);
		}
		
		UserSecGroupRule ret = db.queryForBean(sb.toString(),UserSecGroupRule.class,params);
		String code=db.getErrCode();
		if (!code.equals(SysErr.CODE_SUCCESS)){
			this.log(db.getMsg());
		}
		this.setErrCode(code);
		return ret;
	}
}